import './index.less';
import Utils from "../../../common/utils.js";
// import LoginForm from '../../../components/h5/login';
import Dialog from '../../../components/h5/dialog';
import DyDialog from '../../../components/h5/dyDialog';
import galleryListView from "./index.html";
import headerPartial from "./partial/header.html";
import list3 from "./partial/newslist.html";


// 接口服务
import IndexService from "../../../services/indexService.js";
const indexService = new IndexService();

export default class GalleryList {
    constructor() {
        let _this = this;
        this.request = true;
        this.loginForm = null;
        this.complied = _.template(galleryListView);
        this.compliedHeaderPartial = _.template(headerPartial);
        this.compliedList3 = _.template(list3);
        this.params = '';
        this.start = 1;
        this.length = 10
        this.token = Utils.GetToken();
        console.log("token是", this.token)
        if (this.token == '') {
            alert("无法获取token")
        }

        this.AD = {}
        this.dyURL = ''
        this.render();
        this.isMore = true;
        window.searchNew = function() {
            var s = document.getElementById('search').value;
            if (s === '') {
                return;
            }
            document.getElementById('search').blur();
            var keyword = document.getElementById('search').value
            console.log('搜索' + document.getElementById('search').value)
            window.location = `/search.html?keyword=${keyword}&token=${_this.token}`
        }
    }
    //渲染页面
    async render() {

        let _this = this;
        var liveList = {};
        var liveList2 = {};
        var hpList = {};
        //加载header
        let headerHtml = _this.compliedHeaderPartial({
            data: 1,
            token: _this.token
        });
        //轮播1
        let result = await indexService.getLiveList(this.token);
        console.log("result", result)
        if (result.code == 200) {
            liveList = result.data;
            if (liveList.length >= _this.length) {
                _this.start = _this.start + 1;
            } else {
                //已无更多数据，不再加载。
                _this.isMore = false;
            }
        } else {
            alert(result.message)
        }
        //弹窗
        var level_foreach = false;
        liveList.forEach(item => {
            if (item.status == "living" && !level_foreach) {
                console.log("xin", item)
                // 弹窗
                new Dialog(item.title, item.MobileMainPopup, `/live.html?id=${item.id}&token=${_this.token}`);
                level_foreach = true;
            }
        });



        //AD
        let ADResult = await indexService.getAD(this.token);
        console.log("ADResult", ADResult)
        if (ADResult.code == 200) {
            this.AD = ADResult.data[0];
        } else {
            alert(ADResult.message)
        }
        //轮播2
        let result2 = await indexService.getLiveList2(this.token);
        console.log("result2", result2)
        if (result2.code == 200) {
            liveList2 = result2.data;
        } else {
            console.log("result2.message", result2.message)
        }
        //列表
        let hpResult = await indexService.getHpList(this.token);
        console.log("hpResult", hpResult)
        if (hpResult.code == 200) {
            hpList = hpResult.data;
        } else {
            alert(hpResult.message)
        }
        setTimeout(() => {
            $(".header-container").html(headerHtml);
        }, 20);




        //整个页面列表
        let galleryListHtml = this.complied({
            liveList: liveList,
            liveList2: liveList2,
            ADURL: this.AD.MobileImage,
            hpList: hpList
        });
        $(".content").html(galleryListHtml);
        //更新新闻视图
        let List3Html = _this.compliedList3({
            liveList3: liveList.splice(0, _this.length),
        });
        $(".news_container").html(List3Html);
        this.registerEvents();
        $(".page_container").fadeIn();
        // 顶部新闻轮播
        new Swiper('.submain_container', {
            slidesPerView: 'auto',
            loopedSlides: 3,
        });
        //中部banner切换
        new Swiper('.banner_container', {
            // centeredSlides: true,
            // slidesPerView: 'auto',
            // loopedSlides: 1,
            slidesPerView: 1.1,
            spaceBetween: 5,
            centeredSlides: true,
        });

        //订阅
        let qr = await indexService.getQR(this.token);
        console.log("qr", qr)
        if (qr.code == 200) {
            dyURL = 'data:image/png;base64,' + qr.data.url;
        } else {
            console.log("qr.message", qr.message)
        }


    }

    //注册事件
    registerEvents() {
        let _this = this;



        //切换医院
        $('.tag_group').delegate(".tag", "click", async function(e) {
            console.log(e)
            console.log($(this).attr('data-name'))
            var liveList = {};
            $('.tagLive').removeClass("active");
            $(this).addClass("active");

            if ($(this).attr('data-name') == "all") {
                _this.params = "";
            } else {
                _this.params = $(this).attr('data-name');
            }

            let result = await indexService.getLiveList3({
                token: _this.token,
                HospitalRoleName: _this.params,
                start: 1,
                length: _this.length
            });
            console.log("result", result)
            if (result.code == 200) {
                liveList = result.data;
            } else {
                alert(result.message)
            }
            //更新视图
            let List3Html = _this.compliedList3({
                liveList3: liveList,
            });
            $(".news_container").html(List3Html);
        });


        //点击AD
        $('.page_container').delegate(".longBg", "click", function() {
            window.location = _this.AD.RedirectURL;
        });


        //订阅
        $('.page_container').delegate(".dy1", "click", function() {
            new DyDialog(dyURL);
        });

        //点击list2
        $('.page_container').delegate(".list2_block", "click", function() {
            console.log("hahah")
            window.location = $(this).attr('data-url');
        });

        //点击直播新闻列表
        $('.page_container').delegate(".news", "click", function() {
            var id = $(this).attr('data-id')
            if ($(this).attr('data-status') == 'end') {
                window.location = `/video.html?id=${id}&token=${_this.token}`
            } else {
                window.location = `/live.html?id=${id}&token=${_this.token}`
            }
        });

        //点击轮播1
        $('.page_container').delegate(".listcol", "click", function() {
            var id = $(this).attr('data-id')
            console.log("类型", $(this).attr('data-status'))
            if ($(this).attr('data-status') == 'end') {
                window.location = `/video.html?id=${id}&token=${_this.token}`
            } else {
                window.location = `/live.html?id=${id}&token=${_this.token}`
            }
        });

        //滚动加载
        $(window).scroll(async function() {
            console.log("jiazai11")
            var scrollTop = $(this).scrollTop();
            var scrollHeight = $(document).height();
            var windowHeight = $(this).height();
            if (scrollTop + windowHeight >= scrollHeight) {
                console.log("jiazai")
                if (_this.isMore == false) {
                    console.log("无需加载了")
                    return;
                }
                // galleryAutoLoad();
                let result = await indexService.getLiveList3({
                    token: _this.token,
                    HospitalRoleName: _this.params,
                    start: _this.start,
                    length: _this.length
                });
                console.log("继续加载result", result)
                if (result.code == 200) {
                    liveList = result.data;
                    if (liveList.length >= _this.length) {
                        _this.start = _this.start + 1;
                    } else {
                        //已无更多数据，不再加载。
                        _this.isMore = false;
                    }
                } else {
                    alert(result.message)
                }
                //更新视图
                let List3Html = _this.compliedList3({
                    liveList3: liveList,
                });
                $(".news_container").append(List3Html);
            }
        });


        
    }

}